<?php
# !!!!!!!!!! DON'T MAKE ANY CHANGE IF YOU DON'T KNOW WHAT YOU'RE DOING !!!!!!!!!!
// --------------------------------------------------------------------------------------------------------------
// File Name              :	register.php
// Last Modification Time	:	2008-02-01 24:00
// Last Modified by       :	turker (turker.biz@gmail.com)
// list of modifications	:
//
//	- 2008-02-01 turker
//		* 24:00 file created.
//
// --------------------------------------------------------------------------------------------------------------
if (eregi("register.php",$_SERVER['PHP_SELF'])) {
	header ("Location:../index.php");
	exit;
}
?>
<!--div:icerik -->
    <div id="icerik">
    <p>
<?php
if (isLogged()) {
  echo 'zaten giriş yapmışsınız.';
}
elseif (empty($_POST)) {
  # form action
  $action='index.php?page=register';
  if ($SITE_SEO) $action=$SITE_URL.'/register';
?>
<script type="text/javascript">
$(document).ready(function(){
  $("#reg").click(function(){
    $("#reg").val(" Lütfen Bekleyiniz... ");
    $("#reg").attr("disabled","disabled");

    if ($("#name").val().length>55||$("#name").val().length<6) {
      $("#name").focus();
      alert("<?php echo $_LANG['lostpass']['error2']; ?>");
    }
    else if ($("#email").val().length>55||$("#email").val().length<6) {
      $("#email").focus();
      alert("<?php echo $_LANG['lostpass']['error1']; ?>");
    }
    else if ($("#pass").val().length>15||$("#pass").val().length<5) {
      $("#pass").focus();
      alert("<?php echo $_LANG['login']['error3']; ?>");
    }
    else if ($("#phone").val().length>11||$("#phone").val().length<7) {
      $("#phone").focus();
      alert("<?php echo $_LANG['register']['error1']; ?>");
    }
    else {
      $.ajax({
      	data:"do=checkReg&email="+$("#email").val(),
      	success: function(r) {
      	  if (r!='ok') {
            alert('<?php echo $_LANG['register']['error3']; ?>');
            $("#email").focus();
      	  }
      	  else $("#regForm").submit();
      	}
      });
    }
    $("#reg").val(" <?php echo $_LANG['register']['register']; ?> ");
    $("#reg").attr("disabled","");
  });
});
</script>
    <span class="uyari"><?php echo $_LANG['register']['msg1']; ?></span>
    <form method="post" action="<?php echo $action; ?>" id="regForm" class="box">
      <label>*<?php echo $_LANG['general']['name']; ?>:</label> <input type="text" maxlength="55" name="name" id="name" accesskey="req" /><br />
      <label>*<?php echo $_LANG['general']['email']; ?>:</label> <input type="text" maxlength="55" name="email" id="email" accesskey="req" /><br />
      <label>*<?php echo $_LANG['general']['pass']; ?>:</label> <input type="password" maxlength="15" name="pass" id="pass" accesskey="req" /><br />
      <label><?php echo $_LANG['general']['lang']; ?>:</label> <?php echo listLangs(); ?><br />
      <label><?php echo $_LANG['general']['bdate']; ?>:</label>
      <?php echo $_LANG['general']['day']; ?>: <select name="day" id="day">
      <?php for($i=1;$i<32;$i++) echo '<option value="'.$i.'">'.$i.'</option>'; ?>
      </select>&nbsp;
      <?php echo $_LANG['general']['month']; ?>: <select name="month" id="month">
      <?php for($i=1;$i<13;$i++) echo '<option value="'.$i.'">'.$i.'</option>'; ?>
      </select>&nbsp;
      <?php echo $_LANG['general']['year']; ?>: <select name="year" id="year">
      <?php for($i=1940;$i<1995;$i++) echo '<option value="'.$i.'">'.$i.'</option>'; ?>
      </select><br />
      <label><?php echo $_LANG['general']['gender']; ?>:</label> <select name="gender" id="gender">
      <option value="m"><?php echo $_LANG['general']['male']; ?></option><option value="f"><?php echo $_LANG['general']['female']; ?></option></select><br />
      <label>*<?php echo $_LANG['general']['phone']; ?>:</label> <input type="text" maxlength="11" name="phone" id="phone" accesskey="req" /><br />
      <label><?php echo $_LANG['general']['city']; ?>:</label> <?php echo listCities(); ?><br />
      <label><?php echo $_LANG['general']['address']; ?>:</label> <textarea name="address" id="address"></textarea><br />
      <label><?php echo $_LANG['register']['tax_office']; ?>:</label> <input type="text" maxlength="55" name="tax_office" id="tax_office" /><br />
      <label><?php echo $_LANG['register']['tax_id']; ?>:</label> <input type="text" maxlength="20" name="tax_id" id="tax_id" /><br />
      <label></label><input type="button" value=" <?php echo $_LANG['register']['register']; ?> " id="reg" class="input-submit" />
    </form>
<?php
}
else {
  # prepare $_POST
  foreach ($_POST as $key=>$val) $_POST[$key]=trim($val);
  extract($_POST,EXTR_OVERWRITE);

  # check $_POST
  $valid=new Validation();
  $valid->inRange($name,55,5,$_LANG['lostpass']['error2']);
  $valid->inRange($email,55,6,$_LANG['lostpass']['error1']);
  $valid->inRange($pass,15,5,$_LANG['login']['error3']);
  $valid->inRange($phone,11,7,$_LANG['register']['error1']);
  $valid->checkEmail($email,$_LANG['general']['imail']);
  if (!empty($tax_id)) $valid->isNumber($tax_id,$_LANG['register']['error4']);
  if ($valid->isError()) $valid->listErrors();
  else { #no error
    $email=$mysql->escape($email);
    $q=$mysql->query("select id from $USERS_TABLE where email='$email'");
    if ($mysql->numRows($q)>0) echo $_LANG['register']['error5'].'<br />';
    else {
      foreach ($_POST as $key=>$val) $_POST[$key]=$mysql->escape($val);
      extract($_POST,EXTR_OVERWRITE);
      $pass=md5(strrev(md5($pass)));
      $date=time()+(60*$TIME_CORRECTION);
      $bdate=mktime(0,0,0,$month,$day,$year);
      $level=0;
      if (!$USER_EMAL_VALID) {
        $level=1;
        if (!$USER_APPROVE) $level=2;
      }

      // id, email, password, name, lang, date, level, bdate, gender, phone, address, city, discount, discount_type, tax_id, tax_office
      $sql="'','$email','$pass','$name','$lang','$date','$level','$bdate','$gender','$phone','$address','$city','0','ratio','$tax_id','$tax_office'";
      $q=$mysql->query("insert into $USERS_TABLE values($sql)");
      if ($q) {
        $lastID=$mysql->lastID();
        $code=md5(md5($pass.$date));

        echo '<br />'.$_LANG['register']['ok'].'<br />';
        if ($USER_EMAL_VALID) {
          $message=$ACTIVATION_MAIL_BODY.'<br /><a href="'.$SITE_URL.'/index.php?page=activate&id='.$lastID.'&email='.$email.'&code='.$code.'">'
          .$_LANG['sendact']['actmail']."$SITE_URL/index.php?page=activate&id=$lastID&email=$email&code=$code<br /><br />".$MAIL_SIGNATURE;

          $mail=sendMail($email,$SITE_CONTACT_MAIL,$ACTIVATION_MAIL_HEADER,$message);
          if ($mail) echo $_LANG['register']['readmail'];
          else echo $_LANG['register']['error6'].' '.$_LANG['errors']['error1'].': '.$SITE_ADMIN_MAIL;
        }
        else {
          if(!$USER_APPROVE && $WELCOME_MAIL) {
            $message=$WELCOME_MAIL_BODY.$MAIL_SIGNATURE;
            sendMail($email,$SITE_CONTACT_MAIL,$WELCOME_MAIL_HEADER,$message);
          }
        } // else
      }
      else echo '<br />'.$_LANG['errors']['error'].' - '.$_LANG['errors']['error1'].': '.$SITE_ADMIN_MAIL;
    } // else
  } // else
}
?>
    </p>
    </div>
<!--//div:icerik -->